package cn.yunhe.crm.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.yunhe.crm.annotation.SystemLog;
import cn.yunhe.crm.dao.DeptMapper;
import cn.yunhe.crm.dao.OrganizationMapper;
import cn.yunhe.crm.entity.Aftersale;
import cn.yunhe.crm.entity.Dept;
import cn.yunhe.crm.entity.Emp;
import cn.yunhe.crm.entity.Organization;
import cn.yunhe.crm.entity.Position;
import cn.yunhe.crm.service.AftersaleService;

@Scope("prototype")
@Controller
public class AftersaleController {
	@Qualifier("AftersaleService")
	private AftersaleService asService;
	
	@Autowired
	public void setAsService(AftersaleService asService) {
		this.asService = asService;
	}
	
	
	private DeptMapper dMapper;

	@Autowired
	public void setdMapper(DeptMapper dMapper) {
		this.dMapper = dMapper;
	}
	
	private OrganizationMapper oMapper;
	
	@Autowired
	public void setoMapper(OrganizationMapper oMapper) {
		this.oMapper = oMapper;
	}
	
	/**
	 * 查询所有售后日志信息
	 * @param session
	 * @return
	 */
	@RequestMapping("aftersaleinfo.do")
	public String checkAllAftersale(HttpSession session){
		List<Aftersale> aftersales=asService.checkAllAftersale();
		session.removeAttribute("aftersales");
		session.setAttribute("aftersales", aftersales);
		return "aftersaleinfo";
	}
	
	/**
	 * 查询所有部门以及机构信息
	 * @return
	 */
	@RequestMapping("aftersaleinfoadd.do")
	public String checkAllDeptAndOrganization(HttpSession session){
		List<Dept> depts=dMapper.selectByExample(null);
		List<Organization> organizations=oMapper.selectByExample(null);
		session.removeAttribute("depts");
		session.setAttribute("depts", depts);
		session.removeAttribute("organizations");
		session.setAttribute("organizations", organizations);
		return "aftersaleinfoadd";
	}
	/**
	 * 新增售后
	 * @return
	 */
	@RequestMapping("aftersaleadd.do")
	public String addAftersale(Aftersale aftersale){
		int flag=asService.addAftersale(aftersale);
		if(flag>0){
			return "redirect:/aftersaleinfo.do";
		}else{
			return "aftersaleinfoadd";
		}
	}
	/**
	 * 判断手机号唯一性
	 * @return
	 */
	
	/**
	 * 售后信息详情
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping(value="aftersaleinfodetail.do")
	public String detailAftersale(Integer id,HttpSession session){
		Aftersale aftersale=asService.checkAftersaleById(id);
		session.removeAttribute("aftersale");
		session.setAttribute("aftersale", aftersale);
		return "aftersaleinfodetail";
	}
	/**
	 * 跳转至编辑员工界面
	 * @param id
	 * @param session
	 * @return
	 */
	@RequestMapping(value="aftersaleinfoupdate.do")
	public String toaftersaleinfodetail( Integer id,HttpSession session){
		Aftersale aftersale=asService.checkAftersaleById(id);
		List<Dept> depts=dMapper.selectByExample(null);
		List<Organization> organizations=oMapper.selectByExample(null);
		session.removeAttribute("depts");
		session.setAttribute("depts", depts);
		session.removeAttribute("organizations");
		session.setAttribute("organizations", organizations);
		session.removeAttribute("aftersale");
		session.setAttribute("aftersale", aftersale);
		return "aftersaleinfoupdate";
	}
	
	/**
	 * 编辑售后信息
	 * @return
	 */
	@SystemLog(des="更新售后日志")
	@RequestMapping("aftersaleupdate.do")
	public String updateAftersale(Aftersale aftersale){
		int flag=asService.updateAftersale(aftersale);
		if(flag>0){
			return "redirect:/aftersaleinfo.do";
		}else{
			return "aftersaleinfoupdate";
		}
	}
	
	/**
	 * 删除售后日志
	 * @param id
	 * @return
	 */
	@SystemLog(des="删除售后日志")
	@RequestMapping(value="aftersaleinfodel.do")
	public String delAftersale( Integer id){
		int flag=asService.delAftersale(id);
		if(flag>0){
			return "redirect:/aftersaleinfo.do";
		}else{
			return "aftersaleinfo";
		}
	}
	
}
